//
//  MineUserInfomationTableViewCell.swift
//  LeTu
//
//  Created by gy on 2023/12/6.
//

import UIKit

class MineUserInfomationTableViewCell: BaseTableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.vipIconImgView.layerCornerRadius(self.vipIconImgView.height/2)
            self.avatarBackView.layerCornerRadius(self.avatarBackView.height/2)
            self.avatarImgView.layerCornerRadius(self.avatarImgView.height/2)
            self.joinVipLabel.layerCornerRadius(self.joinVipLabel.height/2)
        }
    }
    static func getCellHeight(isVip: Bool) -> CGFloat {
        var cellHeight = Size.navigationBarWithoutStatusHeight
        
        // 用户信息
        cellHeight += 34 + 60
        // 累计解说
        let arr: ASAttributedString =  MineUserInfomationTableViewCell.getAccumulativePrompt()
        cellHeight += max(arr.value.widthWithSizeThatFits(constrainedToSize: CGSize(width: screenWidth-40, height: CGFloat.greatestFiniteMagnitude), font: kSetPingFangMedium(14))+10, 25) + 16
        // 会员状态
        let vipArr = MineUserInfomationTableViewCell.getVipStatus()
        cellHeight += isVip ? (18 + vipArr.value.widthWithSizeThatFits(constrainedToSize: CGSize(width: screenWidth-40, height: CGFloat.greatestFiniteMagnitude), font: kSetPingFangRegular(14)) + 12) : 0
        // 会员卡片
        let vipCardSize = UIImage(named: "vip_card_background")!
        cellHeight += 10 + screenWidth*vipCardSize.size.height/vipCardSize.size.width + (LanguageManager.shared.currentLanguage == .zh ? 0.0:26.0)

        return cellHeight + 16
    }
    func setUserInfo() {
        
        loginBtn.isHidden = kUserManager.isLogin
        userBackView.isHidden = !loginBtn.isHidden
        
        let isVip = kUserManager.userModel.memberName.localized.count > 0
        let memberName = kUserManager.userModel.memberName
        
        avatarImgView.sd_setImage(with: URL.encodedURL(string: kUserManager.userModel.avatar), placeholderImage: UIImage(named: "avatar_placeholder"))
                
        nicknameLabel.text = kUserManager.userModel.nickname.count > 0 ? kUserManager.userModel.nickname:Utils.hideMiddleDigitsInPhoneNumber(kUserManager.userModel.mobile)
        vipInfolabel.attributed.text = isVip ? "\(.image(UIImage(named: "vip_golden")!, .custom(.offset(CGPoint(x: 0, y: -2)), size: CGSize(width: 20, height: 12))))\(memberName.localized)":""
        
        accumulativePromptLabel.attributed.text = MineUserInfomationTableViewCell.getAccumulativePrompt()
        
        vipStatusBackView.isHidden = !isVip
        vipStatusBackView.snp.updateConstraints { make in
            make.height.equalTo(isVip ? (MineUserInfomationTableViewCell.getVipStatus().value.widthWithSizeThatFits(constrainedToSize: CGSize(width: screenWidth-40, height: CGFloat.greatestFiniteMagnitude), font: kSetPingFangRegular(14)) + 10 + 12):0)
        }
        vipStatusLabel.attributed.text = MineUserInfomationTableViewCell.getVipStatus()
        vipStatusPromptLabel.text = "会员有效期至".localized+"：\(kUserManager.userModel.memberValidDate.components(separatedBy: " ").first ?? kUserManager.userModel.memberValidDate)"
        
        toVipDetailLabel.attributed.text = "\("会员中心".localized) \(.image(UIImage(named: "arrow_golden")!, .custom(.offset(CGPoint(x: 0, y: 1)), size: CGSize(width: 10, height: 8))))"
        
        vipPromptLabel.text = isVip ? (memberName.localized+" "+"无限畅听".localized):"开通会员 无限畅听".localized
        vipDetailLabel.text = isVip ? "您可无限畅听平台内所有音频".localized : "会员可无限畅听平台内所有音频内容".localized
        joinVipLabel.isHidden = isVip
        toVipDetailLabel.isHidden = !isVip
        
        joinVipLabel.text = "立即开通".localized
        loginBtn.setTitle("登录/注册 >".localized)
        
        vipPromptBackView.snp.updateConstraints { make in
            make.height.equalTo(LanguageManager.shared.currentLanguage == .zh ? 72:98)
        }
        
        vipPromptLabel.snp.makeConstraints { make in
            make.top.equalTo(vipPromptIconImgView).offset(LanguageManager.shared.currentLanguage == .zh ? -2:-4)
        }
        
        joinVipLabel.snp.updateConstraints { make in
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 88:108)
        }
        
        toVipDetailLabel.snp.updateConstraints { make in
            make.width.equalTo(toVipDetailLabel.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 18)))
        }
        
    }
    static private func getAccumulativePrompt() -> ASAttributedString {
        let listenNum = LanguageManager.shared.currentLanguage == .zh ? "\(kUserManager.userModel.listenNum)":(" " + "\(kUserManager.userModel.listenNum) " + " ")
        return "\(.image(UIImage(named: "play_icon")!, .custom(.offset(CGPoint(x: 0, y: -3)), size: CGSize(width: 16, height: 16)))) \("累计听了".localized)\(listenNum)\("条解说".localized)"
    }
    static private func getVipStatus() -> ASAttributedString {
        let memberName = kUserManager.userModel.memberName
        return "\("您已开通\(memberName)，可无限畅听平台内所有音频".localized)"
    }
    
    // MARK: - UIButtonAction
    @objc private func loginBtnAction() {
        send(event: MineVC.TransferEvent.loginAction)
    }
    @objc private func vipCardBtnAction() {
        send(event: MineVC.TransferEvent.vipCardAction)
    }
    @objc private func setBtnAction() {
        send(event: MineVC.TransferEvent.setAction)
    }
    
    // MARK: - configUI
    func configUI() {
        
        backView.backgroundColor = .white
        
        contentView.addSubview(backView)
        backView.addSubview(backImgView)
        backView.addSubview(userInfoBackView)
        backView.addSubview(setBtn)
        userInfoBackView.addSubview(avatarBackView)
        userInfoBackView.addSubview(avatarImgView)
        backView.addSubview(vipIconImgView)
        userInfoBackView.addSubview(loginBtn)
        userInfoBackView.addSubview(userBackView)
        userBackView.addSubview(nicknameLabel)
        userBackView.addSubview(vipInfolabel)
        backView.addSubview(accumulativePromptLabel)
        
        backView.addSubview(vipStatusBackView)
        vipStatusBackView.addSubview(vipStatusLabel)
        vipStatusBackView.addSubview(vipStatusPromptLabel)
        
        backView.addSubview(vipPromptBackView)
        vipPromptBackView.addSubview(vipPromptIconImgView)
        vipPromptBackView.addSubview(vipPromptLabel)
        vipPromptBackView.addSubview(vipDetailLabel)
        vipPromptBackView.addSubview(joinVipLabel)
        vipPromptBackView.addSubview(toVipDetailLabel)
        
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        backImgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
        }
        setBtn.snp.makeConstraints { make in
            make.top.equalTo(Size.navigationBarWithoutStatusHeight+10)
            make.right.equalToSuperview()
            make.width.equalTo(64)
            make.height.equalTo(40)
        }
        userInfoBackView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(Size.navigationBarWithoutStatusHeight + 34)
        }
        avatarBackView.snp.makeConstraints { make in
            make.width.height.equalTo(60)
            make.left.top.bottom.equalToSuperview()
        }
        avatarImgView.snp.makeConstraints { make in
            make.left.top.equalTo(avatarBackView).offset(2)
            make.bottom.right.equalTo(avatarBackView).offset(-2)
        }
        vipIconImgView.snp.makeConstraints { make in
            make.centerX.equalTo(avatarImgView.snp.right).offset(-10)
            make.bottom.equalTo(avatarBackView)
            make.width.height.equalTo(20)
        }
        loginBtn.snp.makeConstraints { make in
            make.left.equalTo(avatarBackView.snp.right).offset(10)
            make.centerY.equalToSuperview()
        }
        userBackView.snp.makeConstraints { make in
            make.left.equalTo(loginBtn)
            make.centerY.right.equalToSuperview()
        }
        nicknameLabel.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
        }
        vipInfolabel.snp.makeConstraints { make in
            make.top.equalTo(nicknameLabel.snp.bottom)
            make.left.bottom.right.equalToSuperview()
        }
        accumulativePromptLabel.snp.makeConstraints { make in
            make.left.right.equalTo(userInfoBackView)
            make.top.equalTo(userInfoBackView.snp.bottom).offset(20)
        }
        vipStatusBackView.snp.makeConstraints { make in
            make.left.right.equalTo(userInfoBackView)
            make.top.equalTo(accumulativePromptLabel.snp.bottom).offset(18)
            make.height.equalTo(38)
        }
        vipStatusLabel.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
        }
        vipStatusPromptLabel.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
        }
        vipPromptBackView.snp.makeConstraints { make in
            make.left.right.equalTo(userInfoBackView)
            make.height.equalTo(72)
            make.bottom.equalTo(-16)
        }
        vipPromptIconImgView.snp.makeConstraints { make in
            make.top.equalTo(14)
            make.left.equalTo(10)
        }
        vipPromptLabel.snp.makeConstraints { make in
            make.left.equalTo(vipPromptIconImgView.snp.right).offset(4)
            make.top.equalTo(vipPromptIconImgView).offset(-2)
            make.width.lessThanOrEqualTo(226)
        }
        vipDetailLabel.snp.makeConstraints { make in
            make.left.equalTo(vipPromptLabel)
            make.top.equalTo(vipPromptLabel.snp.bottom).offset(6)
        }
        joinVipLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.width.equalTo(88)
            make.height.equalTo(28)
        }
        toVipDetailLabel.snp.makeConstraints { make in
            make.right.centerY.equalTo(joinVipLabel)
            make.width.equalTo(0)
        }
        
        setBtn.addTarget(self, action: #selector(setBtnAction))
        loginBtn.addTarget(self, action: #selector(loginBtnAction))
        
        let tapGR = UITapGestureRecognizer(target: self, action: #selector(vipCardBtnAction))
        vipPromptBackView.addGestureRecognizer(tapGR)
        vipPromptBackView.isUserInteractionEnabled = true
        
    }
    private lazy var backView = UIView()
    private lazy var backImgView = UIImageView(image: UIImage(named: "mine_head_background"))
    // 设置
    private var setBtn: CustomClickableView = {
        let setBtn = CustomClickableView()
        setBtn.setImage(UIImage(named: "set"))
        return setBtn
    }()
    // 用户信息
    private lazy var userInfoBackView = UIView()
    // - 头像
    private lazy var avatarBackView: UIView = {
        let avatarBackView = UIView()
        avatarBackView.backgroundColor = UIColor(red: 0.879, green: 0.781, blue: 0.59, alpha: 1)
        return avatarBackView
    }()
    private lazy var avatarImgView: UIImageView = {
        let avatarImgView = UIImageView()
        avatarImgView.contentMode = .scaleAspectFill
        avatarImgView.image = UIImage(named: "avatar_placeholder")
        return avatarImgView
    }()
    private lazy var vipIconImgView: UIButton = {
        let vipIconImgView = UIButton()
        vipIconImgView.isEnabled = false
        vipIconImgView.backgroundColor = UIColor(red: 0.945, green: 0.992, blue: 0.988, alpha: 1)
        vipIconImgView.setImage(UIImage(named: "vip_white"), for: .disabled)
        vipIconImgView.imageEdgeInsets = UIEdgeInsets(top: 6, left: 5, bottom: 6, right: 3)
        return vipIconImgView
    }()
    // - 登录
    private lazy var loginBtn: CustomClickableView = {
        let loginBtn = CustomClickableView()
        loginBtn.setFont(kSetPingFangMedium(18))
        loginBtn.setTitleColor(.app_black)
        loginBtn.isHidden = true
        return loginBtn
    }()
    // - 手机号 & vip信息
    private lazy var userBackView = UIView()
    private lazy var nicknameLabel: UILabel = {
        let nicknameLabel = UILabel()
        nicknameLabel.font = kSetPingFangMedium(18)
        return nicknameLabel
    }()
    private lazy var vipInfolabel: UILabel = {
        let vipInfolabel = UILabel()
        vipInfolabel.font = kSetPingFangRegular(12)
        vipInfolabel.textColor = .app_lightGolden
        return vipInfolabel
    }()
    // - 累计播放
    private lazy var accumulativePromptLabel: UILabel = {
        let accumulativePromptLabel = UILabel()
        accumulativePromptLabel.font = kSetPingFangMedium(14)
        accumulativePromptLabel.numberOfLines = 0
        return accumulativePromptLabel
    }()
    // - 会员时长
    private lazy var vipStatusBackView = UIView()
    private lazy var vipStatusLabel: UILabel = {
        let vipStatusLabel = UILabel()
        vipStatusLabel.font = kSetPingFangRegular(14)
        vipStatusLabel.numberOfLines = 0
        return vipStatusLabel
    }()
    private lazy var vipStatusPromptLabel: UILabel = {
        let vipStatusPromptLabel = UILabel()
        vipStatusPromptLabel.font = kSetPingFangRegular(12)
        vipStatusPromptLabel.textColor = .app_detaisltextColor
        return vipStatusPromptLabel
    }()
    // - 会员卡片
    private lazy var vipPromptBackView = UIImageView(image: UIImage(named: "vip_card_background"))
    private lazy var vipPromptIconImgView = UIImageView(image: UIImage(named: "vip_dark_golden"))
    private lazy var vipPromptLabel: UILabel = {
        let vipPromptLabel = UILabel()
        vipPromptLabel.font = kSetPingFangMedium(16)
        vipPromptLabel.numberOfLines = 0
        return vipPromptLabel
    }()
    private lazy var vipDetailLabel: UILabel = {
        let vipDetailLabel = UILabel()
        vipDetailLabel.font = kSetPingFangRegular(12)
        return vipDetailLabel
    }()
    private lazy var joinVipLabel: UILabel = {
        let joinVipLabel = UILabel()
        joinVipLabel.font = kSetPingFangMedium(14)
        joinVipLabel.textColor = .app_white
        joinVipLabel.textAlignment = .center
        joinVipLabel.backgroundColor = .app_themeColor
        return joinVipLabel
    }()
    private lazy var toVipDetailLabel: UILabel = {
        let toVipDetailLabel = UILabel()
        toVipDetailLabel.font = kSetPingFangMedium(12)
        toVipDetailLabel.textColor = .app_golden
        return toVipDetailLabel
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
